package club.yunqiang.service.impl;

import club.yunqiang.dto.ModifyDBInstanceConnection;
import club.yunqiang.dto.ModifyDBInstanceNetworkExpireTime;
import club.yunqiang.properties.RdsProperties;
import club.yunqiang.service.RdsSdkService;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.rds.model.v20140815.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author YunQiang
 */
@Service
public class RdsSdkServiceImpl implements RdsSdkService {

    /**
     * 阿里API请求客户端
     */
    private IAcsClient client;

    /**
     * 核心配置类(因为该参数要用于初始化，所以使用构造器注入)
     */
    private RdsProperties rdsProperties;

    @Autowired
    public RdsSdkServiceImpl(RdsProperties rdsProperties) {
        this.rdsProperties = rdsProperties;
        // init 方法必须在参数 rdsProperties 初始化之后
        init();
    }

    /**
     * 初始化请求客户端
     */
    private void init(){
        DefaultProfile profile = DefaultProfile.getProfile(rdsProperties.getRegionId(), rdsProperties.getAccessKeyId(), rdsProperties.getSecret());
        client = new DefaultAcsClient(profile);
    }

    @Override
    public AllocateInstancePublicConnectionResponse allocateInstancePublicConnection(String connectionStringPrefix) {
        //设置参数
        AllocateInstancePublicConnectionRequest request = new AllocateInstancePublicConnectionRequest();
        request.setPort(rdsProperties.getPort());
        request.setConnectionStringPrefix(connectionStringPrefix);
        request.setDBInstanceId(rdsProperties.getDbInstanceId());

        AllocateInstancePublicConnectionResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new RuntimeException(e.getMessage(),e);
        }

        return response;
    }

    @Override
    public DescribeDBInstanceNetInfoResponse describeDBInstanceNetInfo() {
        //设置参数
        DescribeDBInstanceNetInfoRequest request = new DescribeDBInstanceNetInfoRequest();
        request.setDBInstanceId(rdsProperties.getDbInstanceId());

        DescribeDBInstanceNetInfoResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new RuntimeException(e.getMessage(),e);
        }

        return response;
    }

    @Override
    public ModifyDBInstanceConnectionStringResponse modifyDBInstanceConnectionString(ModifyDBInstanceConnection modifyDBInstanceConnection) {
        //设置参数
        ModifyDBInstanceConnectionStringRequest request = new ModifyDBInstanceConnectionStringRequest();
        request.setConnectionStringPrefix(modifyDBInstanceConnection.getConnectionStringPrefix());
        request.setCurrentConnectionString(modifyDBInstanceConnection.getCurrentConnectionString());
        request.setDBInstanceId(rdsProperties.getDbInstanceId());
        request.setPort(modifyDBInstanceConnection.getPort());

        ModifyDBInstanceConnectionStringResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new RuntimeException(e.getMessage(),e);
        }

        return response;
    }

    @Override
    public ModifyDBInstanceNetworkExpireTimeResponse modifyDBInstanceNetworkExpireTime(ModifyDBInstanceNetworkExpireTime modifyDBInstanceNetworkExpireTime){
        ModifyDBInstanceNetworkExpireTimeRequest request = new ModifyDBInstanceNetworkExpireTimeRequest();
        request.setDBInstanceId(rdsProperties.getDbInstanceId());
        request.setConnectionString(modifyDBInstanceNetworkExpireTime.getConnectionString());
        request.setClassicExpiredDays(modifyDBInstanceNetworkExpireTime.getClassicExpiredDays());


        ModifyDBInstanceNetworkExpireTimeResponse response = null;
        try {
            response =  client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new RuntimeException(e.getMessage(),e);
        }

        return response;
    }

    @Override
    public ModifyDBInstanceNetworkTypeResponse modifyDBInstanceNetworkType(String instanceNetworkType) {
        ModifyDBInstanceNetworkTypeRequest request = new ModifyDBInstanceNetworkTypeRequest();
        request.setDBInstanceId(rdsProperties.getDbInstanceId());
        // Classic：表示经典网络、VPC：表示VPC网络
        request.setInstanceNetworkType(instanceNetworkType);

        ModifyDBInstanceNetworkTypeResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new RuntimeException(e.getMessage(),e);
        }

        return response;
    }

    @Override
    public ReleaseInstancePublicConnectionResponse releaseInstancePublicConnection(String currentConnectionString) {
        ReleaseInstancePublicConnectionRequest request = new ReleaseInstancePublicConnectionRequest();
        request.setCurrentConnectionString(currentConnectionString);
        request.setDBInstanceId(rdsProperties.getDbInstanceId());

        ReleaseInstancePublicConnectionResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new RuntimeException(e.getMessage(),e);
        }

        return response;
    }

    @Override
    public SwitchDBInstanceNetTypeResponse switchDBInstanceNetType(String connectionStringPrefix) {
        SwitchDBInstanceNetTypeRequest request = new SwitchDBInstanceNetTypeRequest();
        request.setDBInstanceId(rdsProperties.getDbInstanceId());
        request.setConnectionStringPrefix(connectionStringPrefix);

        SwitchDBInstanceNetTypeResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
			throw new RuntimeException(e.getMessage(),e);
        }

        return response;
    }
}
